<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input v-model="queryInfo.query">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单信息表格区域 -->
      <el-table :data="orderList" stripe border>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column
          label="订单价格"
          prop="order_price"
          width="120"
        ></el-table-column>
        <el-table-column label="是否付款" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.order_pay === '0'" type="danger" size="mini"
              >未付款</el-tag
            >
            <el-tag v-else type="success" size="mini">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="是否发货"
          prop="is_send"
          width="100"
        ></el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="160">
          <template slot-scope="scope">
            {{ scope.row.create_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="editAdress"
            ></el-button>
            <el-button
              type="success"
              size="mini"
              icon="el-icon-location"
              @click="logisticsInfo"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 页码区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="adressDialogVisible"
      width="50%"
      @close="adressDialogClosed"
    >
      <el-form
        ref="adressFormRef"
        :rules="adressFormRules"
        :model="adressForm"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="adress1">
          <el-cascader
            :options="cityData"
            v-model="adressForm.adress1"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="adress2">
          <el-input v-model="adressForm.adress2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="adressDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 显示物流信息对话框 -->
    <el-dialog
      :visible-sync="logisticsDialogVisible"
      width="50%"
      title="查看物流信息"
    >
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in logisticsList"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  data() {
    return {
      orderList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      /* 控制修改地址对话框的显示与隐藏 */
      adressDialogVisible: false,
      /* 修改地址对话框表单 */
      adressForm: {
        adress1: [],
        adress2: '',
      },
      adressFormRules: {
        adress1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' },
        ],
        adress2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      cityData,
      /* 控制物流对话框的显示与隐藏 */
      logisticsDialogVisible: false,
      logisticsList: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单信息失败！')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    /* 页码大小 */
    handleSizeChange(pageSize) {
      this.queryInfo.pagesize = pageSize
      this.getOrderList()
    },
    /* 当前页码 */
    handleCurrentChange(pageNum) {
      this.queryInfo.pagenum = pageNum
      this.getOrderList()
    },
    /* 显示修改地址对话框 */
    editAdress() {
      this.adressDialogVisible = true
    },
    /* 修改地址对话框关闭时清空表单数据 */
    adressDialogClosed() {
      this.$refs.adressFormRef.resetFields()
    },
    /* 显示物流信息对话框 */
    async logisticsInfo() {
      this.$message.error('借口文档错误！')
      /* const {data:res} = await this.$http.get('/kuaidi/804909574412544580')
      console.log(res)
      if(res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败！')
      }
      this.logisticsList = res.data
      this.logisticsDialogVisible = true */
    },
  },
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>